{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>total_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>9.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2.6</td>\n",
       "      <td>16.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>55.55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   passenger_count  trip_distance  total_amount\n",
       "0                1            1.5          9.95\n",
       "1                1            2.6         16.30\n",
       "2                3            0.0          5.80\n",
       "3                5            0.0          7.55\n",
       "4                5            0.0         55.55"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = '../data/nyc_taxi_2019-01.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['passenger_count',\n",
    "                         'trip_distance', 'total_amount'])\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "passenger_count\n",
       "0    18.663658\n",
       "1    15.609601\n",
       "2    15.831294\n",
       "3    15.604015\n",
       "4    15.650307\n",
       "5    15.546940\n",
       "6    15.437892\n",
       "7    48.278421\n",
       "8    64.105517\n",
       "9    31.094444\n",
       "Name: total_amount, dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# For each number of passengers, find the mean cost of a taxi ride.\n",
    "# Sort this result from lowest (i.e., cheapest) to highest (i.e., most expensive).\n",
    "\n",
    "df.groupby('passenger_count')['total_amount'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "passenger_count\n",
       "0    18.663658\n",
       "1    15.609601\n",
       "2    15.831294\n",
       "3    15.604015\n",
       "4    15.650307\n",
       "5    15.546940\n",
       "6    15.437892\n",
       "7    48.278421\n",
       "8    64.105517\n",
       "9    31.094444\n",
       "Name: total_amount, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Sort the results once again, in increasing number of passengers.\n",
    "\n",
    "df.groupby('passenger_count')['total_amount'].mean().sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "trip_distance_group\n",
       "long      1.590035\n",
       "medium    1.585319\n",
       "short     1.558400\n",
       "Name: passenger_count, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Now create a new column, `trip_length`, in which the values will be `short`\n",
    "# (< 2 miles), `medium` (> 2 miles and <= 10 miles), or `long` (> 10 miles). \n",
    "# What was the average number of passengers per trip length category? Sort \n",
    "# this result from highest (greatest number of passengers) to lowest\n",
    "# (smallest number of passengers).\n",
    "\n",
    "df['trip_distance_group'] = pd.cut(df['trip_distance'], \n",
    "                                   [df['trip_distance'].min(), 2, 10, \n",
    "                                    df['trip_distance'].max()],\n",
    "                                  labels=['short', 'medium', 'long'])\n",
    "df.groupby('trip_distance_group')['passenger_count'].mean().sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
